﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LGW.DTO;

public partial class _Default : System.Web.UI.Page
{
    // IList<OrderItems> oIListOrderItems;
    Dictionary<string, OrderItems> odicOrderItems;

    protected void Page_Load(object sender, EventArgs e)
    {



        if (!IsPostBack)
        {
            Session["OrderItems"] = null;
        }

        // oIListOrderItems = Session["OrderItems"] == null ? new List<OrderItems>() : (List<OrderItems>)Session["OrderItems"];
        odicOrderItems = Session["OrderItems"] == null ? new Dictionary<string, OrderItems>() : (Dictionary<string, OrderItems>)Session["OrderItems"];

    }

    protected void BindData()
    {
        gvOrderItems.DataSource = odicOrderItems.Values;
        gvOrderItems.DataBind();
        CalculateFooterBalances();
    }


    protected void AddOrderItems()
    {

        OrderItems objorderItems = new OrderItems();
        objorderItems.GoldRate = 2750;
        objorderItems.orderItemkey = "111_" + DateTime.Now.ToString("ddMMyyyyhhmmssfff");
        objorderItems.ItemCategoryId = 1;
        objorderItems.ItemCode = "NPG";
        objorderItems.ItemName = "NallaPusala Golusus adada";
        objorderItems.ItemDesc = "NallaPusala Golusus dsadsa";
        objorderItems.Quantity = 1;
        objorderItems.Weight = Convert.ToDecimal(1.000);
        objorderItems.Wastage = Convert.ToDecimal(0.100);
        objorderItems.MakingCharge = Convert.ToDecimal(1000);
        objorderItems.OtherCharges = Convert.ToDecimal(100.35);
        objorderItems.TotalWeight = objorderItems.Weight + objorderItems.Wastage;
        objorderItems.GoldCost = (objorderItems.TotalWeight * objorderItems.GoldRate);
        objorderItems.Size = Convert.ToDecimal(24.5);
        objorderItems.SizeUnitId = 2;
        //Stones details
        objorderItems.StonesDesc = "CZ & RUBES Stones";
        objorderItems.StonesCount = 50;
        objorderItems.StonesWeight = Convert.ToDecimal(1.5);
        objorderItems.StonesUnitId = 14;
        objorderItems.StonesCost = Convert.ToDecimal(2560.50);
        objorderItems.NetCost = (objorderItems.GoldCost + objorderItems.MakingCharge + objorderItems.StonesCost + objorderItems.OtherCharges);
        objorderItems.StatusId = (int)Common.Status.ACTIVE;
        objorderItems.PetaluId = 11;
        objorderItems.OrderItemId = 1;
        //  oIListOrderItems.Add(objorderItems);
        odicOrderItems.Add(objorderItems.orderItemkey, objorderItems);
        Session["OrderItems"] = odicOrderItems;
      BindData();
        //  (new LGW.BLL.Products()).CreateProduct(objorderItems);
      //  (new LGW.BLL.Products()).UpdateProduct(objorderItems);

    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        AddOrderItems();


    }

    private void CalculateFooterBalances()
    {
        gvOrderItems.FooterRow.Cells[5].Text = "గ్రా: " + odicOrderItems.Values.Sum(pkg => pkg.Weight).ToString("F3");
        gvOrderItems.FooterRow.Cells[6].Text = "గ్రా: " + odicOrderItems.Values.Sum(pkg => pkg.Wastage).ToString("F3");
        gvOrderItems.FooterRow.Cells[7].Text = "గ్రా: " + odicOrderItems.Values.Sum(pkg => pkg.TotalWeight).ToString("F3");
        gvOrderItems.FooterRow.Cells[3].Text = CurrencyGenrator.CurrencyFormat(odicOrderItems.Values.Sum(pkg => pkg.MakingCharge).ToString());
        gvOrderItems.FooterRow.Cells[4].Text = CurrencyGenrator.CurrencyFormat(odicOrderItems.Values.Sum(pkg => pkg.StonesCost).ToString());
        gvOrderItems.FooterRow.Cells[8].Text = CurrencyGenrator.CurrencyFormat(odicOrderItems.Values.Sum(pkg => pkg.GoldCost).ToString());
        gvOrderItems.FooterRow.Cells[9].Text = CurrencyGenrator.CurrencyFormat(odicOrderItems.Values.Sum(pkg => pkg.OtherCharges).ToString());
        gvOrderItems.FooterRow.Cells[10].Text = CurrencyGenrator.CurrencyFormat(odicOrderItems.Values.Sum(pkg => pkg.NetCost).ToString());

    }
    protected void gvOrderItems_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton imgbtnDelete = (ImageButton)e.Row.FindControl("imgbtnDelete");
            imgbtnDelete.Attributes.Add("onclick", "javascript:return " +
            "confirm('Are you sure you want to delete this orderItemId : " +
            DataBinder.Eval(e.Row.DataItem, "orderItemkey") + "')");
        }
    }
    protected void gvOrderItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        odicOrderItems.Remove(gvOrderItems.DataKeys[e.RowIndex].Value.ToString());
        Session["OrderItems"] = odicOrderItems;
        BindData();
    }

}